package cn.txun05.csmall.product.service.impl;

import cn.txun05.csmall.commons.ex.ServiceException;
import cn.txun05.csmall.product.cache.IAlbumCacheRepository;
import cn.txun05.csmall.product.mapper.AlbumMapper;
import cn.txun05.csmall.product.mapper.PictureMapper;
import cn.txun05.csmall.product.pojo.entity.Album;
import cn.txun05.csmall.product.pojo.entity.Picture;
import cn.txun05.csmall.product.pojo.param.AlbumAddNewParam;
import cn.txun05.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.txun05.csmall.product.pojo.vo.AlbumListItemVo;
import cn.txun05.csmall.product.pojo.vo.AlbumStandardVo;
import cn.txun05.csmall.commons.pojo.vo.PageData;
import cn.txun05.csmall.product.service.IAlbumService;
import cn.txun05.csmall.commons.util.PageInfoToPageDateConverter;
import cn.txun05.csmall.commons.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Override
    public void addNew(AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加相册】的业务，参数：{}",albumAddNewParam);
        //检查相册名称是否被占用，如果被占用 则抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",albumAddNewParam.getName());//name="XXXXXX"
        int countByName = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}",countByName);
        if(countByName>0){
            String message= "添加相册失败，相册名称已经存在!";
            log.warn(message);
            //System.out.println(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //将相册数据写入数据库中
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewParam,album);
        album.setGmtCreate(LocalDateTime.now());
        album.setGmtModified(LocalDateTime.now());
        log.debug("准备将新的相册数据写入到数据库，数据：{}",album);
        albumMapper.insert(album);
        log.debug("将新的相册数据写入到数据库中，完成！");

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除相册】的业务，参数：{}",id);
        //检查相册是否存在，如果不存在，抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countByID = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册ID统计匹配的相册数量，结果：{}",countByID);
        if(countByID==0){
            String message= "删除相册失败，相册数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查是否有图片关联到此相册，如果存在，则抛出异常
        QueryWrapper<Picture> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("album_id",id);
        int countByAlbumID = pictureMapper.selectCount(queryWrapper2);
        log.debug("根据相册ID统计匹配的图片数量，结果：{}",countByAlbumID);
        if(countByAlbumID>0){
            String message= "删除相册失败，仍有图片关联到此相册!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查是否SPU关联到此相册，如果存在，抛出异常

        //检查是否SKU关联到此相册，如果存在，抛出异常


        //删除操作
        albumMapper.deleteById(id);

        albumCacheRepository.delete(id);



    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册详情】的业务，ID:{},新数据：{}",id,albumUpdateInfoParam);
        //检查相册是否存在，不存在抛异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countByID = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册ID统计匹配的相册数量，结果：{}",countByID);
        if(countByID==0){
            String message= "修改册详情失败，相册数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //修改的名称不能和其他相册的名称重复
        QueryWrapper<Album> queryWrapper2 = new QueryWrapper<>();
        //where name=? and id<>?
        queryWrapper2.eq("name",albumUpdateInfoParam.getName()).ne("id",id);
        int countByName= albumMapper.selectCount(queryWrapper2);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}",countByName);
        if(countByName>0){
            String message= "修改册详情失败，相册名称被占用了!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //执行修改
        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateInfoParam,album);
        album.setId(id);
        albumMapper.updateById(album);
        log.debug("将新的相册数据更新入数据库，完成！");
    }
    @Autowired
    private IAlbumCacheRepository albumCacheRepository;

    @Override
    public AlbumStandardVo getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册详情】的业务，参数：{}",id);
        //AlbumStandardVo queryResult = albumMapper.getStandardById(id);
        AlbumStandardVo queryResult = albumCacheRepository.getStandardById(id);
        //有可能查不到数据
        if(queryResult==null){
            String message = "查询相册详情失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public PageData<AlbumListItemVo> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<AlbumListItemVo> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询相册列表】的业务，页码：{}，每条记录数：{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<AlbumListItemVo> list = albumMapper.list();
        PageInfo<AlbumListItemVo> pageInfo = new PageInfo<>(list);
        PageData<AlbumListItemVo> pageData= PageInfoToPageDateConverter.converter(pageInfo);
        log.debug("查询完成，即将返回：{}",pageData);
        return pageData;
    }
}
